package com.kzw.oa.service.system.impl;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.kzw.core.service.impl.BaseServiceImpl;
import com.kzw.core.util.AppUtil;
import com.kzw.core.util.ContextUtil;
import com.kzw.core.web.paging.PagingBean;
import com.kzw.oa.core.dynamicPwd.HttpClient;
import com.kzw.oa.core.dynamicPwd.YooeResponse;
import com.kzw.oa.dao.system.AppUserDao;
import com.kzw.oa.model.system.AppRole;
import com.kzw.oa.model.system.AppUser;
import com.kzw.oa.model.system.Department;
import com.kzw.oa.model.system.IndexDisplay;
import com.kzw.oa.model.system.PanelItem;
import com.kzw.oa.model.system.SysConfig;
import com.kzw.oa.model.system.UserOrg;
import com.kzw.oa.service.ext.DepartmentService2;
import com.kzw.oa.service.system.AppUserService;
import com.kzw.oa.service.system.IndexDisplayService;
import com.kzw.oa.service.system.SysConfigService;

/**
 * @description 用户信息
 * @class AppUserServiceImpl
 * @author 掌新软件
 * @updater YHZ
 * @company KZW
 * @data 2010-12-27AM
 */
public class AppUserServiceImpl extends BaseServiceImpl<AppUser> implements
		AppUserService {
	private AppUserDao dao;
	@Resource
	IndexDisplayService indexDisplayService;
	@Autowired
	private DepartmentService2 deptService;

	public AppUserServiceImpl(AppUserDao dao) {
		super(dao);
		this.dao = dao;
	}

	@Resource
	private SysConfigService sysConfigService;

	@Override
	public AppUser findByUserName(String username) {
		return dao.findByUserName(username);
	}

	@Override
	public List<AppUser> findByDepartment(String path, PagingBean pb) {
		return dao.findByDepartment(path, pb);
	}

	@Override
	public List<AppUser> findByDepartment(String path, Integer roleType,
			PagingBean pb) {
		return dao.findByDepartment(path, roleType, pb);
	}

	@Override
	public List<AppUser> findByDepartment(String path, String userIds,
			PagingBean pb) {
		return dao.findByDepartment(path, pb);
	}

	@Override
	public List<AppUser> findByRole(Long roleId, PagingBean pb) {
		return dao.findByRole(roleId, pb);
	}

	public List<AppUser> findByRoleId(Long roleId) {
		return dao.findByRole(roleId);
	}

	@Override
	public List<AppUser> findSubAppUser(String path, Set<Long> userIds,
			PagingBean pb) {
		return dao.findSubAppUser(path, userIds, pb);
	}

	@Override
	public List<AppUser> findSubAppUserByRole(Long roleId, Set<Long> userIds,
			PagingBean pb) {
		return dao.findSubAppUserByRole(roleId, userIds, pb);
	}

	@Override
	public List<AppUser> findByDepId(Long depId) {
		return dao.findByDepId(depId);
	}

	public String initDynamicPwd(HashMap<String, String> input, String function) {
		SysConfig dynamicPwdConfig = sysConfigService.findByKey("dynamicUri");
		URI base_uri = URI.create(dynamicPwdConfig.getDataValue());
		HttpClient client = new HttpClient(base_uri);
		try {

			YooeResponse response = client.call_api(function, input);

			String ret_cmd = response.getRetCmd();
			logger.debug("=============dynamicPwd status:" + ret_cmd);

			HashMap<String, String> output = response.getVarsDict();
			Iterator<String> i = output.keySet().iterator();
			String result = output.get("ret");

			while (i.hasNext()) {
				String name = i.next();
				String value = output.get(name);
				logger.debug("==============dynamicPwd info:" + name + "="
						+ value);
			}

			return result;

		} catch (IOException e) {
			e.printStackTrace();
			return "\"" + function + "\"失败，异常：" + e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "\"" + function + "\"失败，异常：" + e.getMessage();
		}
	}

	/**
	 * 按角色ID查找用户
	 * 
	 * @param roleIds
	 * @return
	 */
	public List<AppUser> findUsersByRoleIds(String roleIds) {
		return dao.findUsersByRoleIds(roleIds);
	}

	/**
	 * 按角色取得用户列表
	 * 
	 * @param roleId
	 * @return
	 */
	public List<AppUser> getUsersByRoleId(Long roleId) {
		return dao.getUsersByRoleId(roleId);
	}

	/**
	 * 返回当前用户的信息，以Json格式返回 需要返回用户所在公司信息
	 * 
	 * @return
	 */
	public String getCurUserInfo() {
		AppUser currentUser = ContextUtil.getCurrentUser();
		String rolestr = ";";
		Set<AppRole> roles = currentUser.getRoles();
		if (roles.size() > 0) {
			for (AppRole r : roles) {
				rolestr += r.getRoleId() + ";";
			}
		}
		Department curDep = currentUser.getDepartment();
		if (curDep == null) {// 若所属部门为空，则设置一个缺省的部门 TODO
			curDep = new Department();
			curDep.setDepId(0l);
			curDep.setDepName(AppUtil.getCompanyName());
		}
		// 去掉公共权限
		List<IndexDisplay> list = indexDisplayService.findByUser(currentUser
				.getUserId());
		List<PanelItem> items = new ArrayList<PanelItem>();
		for (IndexDisplay id : list) {
			PanelItem pi = new PanelItem();
			pi.setPanelId(id.getPortalId());
			pi.setColumn(id.getColNum());
			pi.setRow(id.getRowNum());
			items.add(pi);
		}
		StringBuffer sb = new StringBuffer();
		sb.append("{success:true,user:{userId:'")
				.append(currentUser.getUserId()).append("',fullname:'")
				.append(currentUser.getFullname()).append("',username:'")
				.append(currentUser.getUsername()).append("',depId:'")
				.append(curDep.getDepId()).append("',roleType:")
				.append(currentUser.getRoleType()).append(",depName:'")
				.append(curDep.getDepName()).append("',roleIds:'")
				.append(rolestr).append("',rights:'");
		sb.append(currentUser.getRights().toString().replace("[", "")
				.replace("]", ""));

		// 增加公司信息
		// sb.append("',orgId:'").append(company.getOrgId()).append("',orgName:'")
		// .append(company.getOrgName()).append("',orgShortName:'").append(company.getShortName());

		Gson gson = new Gson();
		sb.append("',topModules:");
		sb.append(gson.toJson(currentUser.getTopModules().values()));
		sb.append(",items:").append(gson.toJson(items).toString());
		sb.append("},sysConfigs:{");
		// 系统配置也在此时加载
		List<SysConfig> sysConfigs = sysConfigService.getAll();
		for (SysConfig sysConfig : sysConfigs) {
			sb.append("'").append(sysConfig.getConfigKey()).append("':'")
					.append(sysConfig.getDataValue()).append("',");
		}
		if (sysConfigs.size() > 0) {
			sb.deleteCharAt(sb.length() - 1);
		}
		sb.append("}}");

		return sb.toString().replaceAll("\"", "'");
	}

	/**
	 * 按部门取得用户列表
	 * 
	 * @param orgPath
	 * @return
	 */
	@Override
	public List<AppUser> getDepUsers(String orgPath, PagingBean pb,
			Map<String, String> map) {
		return dao.getDepUsers(orgPath, pb, map);
	}

	/**
	 * 取得组织主要负责人
	 * 
	 * @param userOrg
	 * @return
	 */
	@Override
	public List<AppUser> getChargeOrgUsers(Set<UserOrg> userOrgs) {
		return dao.getChargeOrgUsers(userOrgs);
	}

	@Override
	public Boolean isSuperMan(Long userId) {
		return dao.isSuperMan(userId);
	}

	/**
	 * 获得地州：（3级）
	 * */
	@Override
	public Department getOrg(AppUser user) {
		Long orgId = user.getOrgId();
		String path = deptService.get(orgId).getPath();
		String str[] = path.split("[.]");
		Long i;

		if (str.length >= 3) {
			i = Long.parseLong(str[2]);
		} else {
			i = 1L;
		}
		return deptService.get(i);
	}

}
